Main site

Built-in Modules > processShopifyWebhook Function

processShopifyWebhook Function

Overview

The processShopifyWebhook function is an optional asynchronous function that processes webhook data received from Shopify. It handles the incoming WEBHOOK_DATA JSON payload and returns an acknowledgment to confirm successful processing. This ensures Shopify does not resend duplicate webhook notifications.

You can use this function to perform actions based on Shopify events such as order creation or fulfillment. Currently, it supports the following webhook topics:
"orders/create", "orders/edited", "orders/fulfilled", "orders/cancelled", "orders/paid","draft_orders/create", and "draft_orders/update".

Function Signature

export async function processShopifyWebhook(WEBHOOK_DATA, env)

Arguments

  • WEBHOOK_DATA (Object): The JSON payload sent by Shopify containing event-specific data.
    This includes a webhook_topic field with one of the following values:
    "orders/create", "orders/edited", "orders/fulfilled", "orders/cancelled", "orders/paid", "draft_orders/create", or "draft_orders/update".
    Use this field to identify the event type when handling multiple webhook triggers.

  • env (Object): An object containing your app's environment variables.
    These are encrypted variables saved in your JsRates app Settings page (e.g., webhook_secret).

Notes

  • The processShopifyWebhook function is optional, but if you implement it, the function needs to return { success: true } to acknowledge receipt and processing of the webhook. If not, Shopify may retry sending the webhook multiple times.
  • All received webhook data is automatically logged and can be viewed in your JsRates Editor page under: More actions > Logs.

Usage Example

import { print } from "./moduels.js"
export async function processShopifyWebhook(WEBHOOK_DATA, env) {
  try {
    // Process the webhook data here
    // Example: extract the order ID or customer info and trigger further actions
    // Use the built-in "print" function to log data for debugging

    return { success: true }; // Prevents Shopify from retrying the webhook
  } catch (err) {
    print("Webhook Error", err);
    return { success: false }; // Triggers retries from Shopify
  }
}

Shopify Webhook Setup Guide

You can create webhook triggers in your Shopify store admin as follows:

  1. Go to Settings > Notifications > Webhooks.
  2. Click Create webhook.
  3. Select the event you want to listen for (e.g., Order creation).
  4. Set Format to JSON.
  5. Enter the URL for your JsRates webhook endpoint:
    https://api.jsrates.com/shopify/{store shopify domain without ".myshopify.com"}-shipping
    
    Example:
    If your Shopify store domain is myshop.myshopify.com, use:
    https://api.jsrates.com/shopify/myshop-shipping
    
  6. Select the Webhook API version as Latest.
  7. After creating the webhook, copy the Webhook signature secret (a long string at the bottom of the Webhooks section).
  8. Go to your JsRates Settings page and add a new secret variable:
    • Key: webhook_secret
    • Value: (paste your Webhook signature secret)

This secret is used to verify and authenticate incoming webhook data.

Example Workflow in JsRates Editor

In your JsRates Editor, add the processShopifyWebhook function outside your existing calculateShippingRates(DATA, env) function.

// This is the mandatory shipping rate calculator function
// You can't remove this
export async function calculateShippingRates(DATA, env){
    //Shipping rate calculator logic
    return {rates:[{...}]}
}

// This is an optional function to process Shopify webhook data.
// Example use case: send order details to your carrier for fulfillment.
export async function processShopifyWebhook(WEBHOOK_DATA, env) {
  try {    
    // Your custom logic here
    // For example, log the order creation event, send a notification, etc.
    // print("Webhook DATA", WEBHOOK_DATA);

    return { success: true };
  } catch (err) {
    //print("Webhook Error", err);
    return { success: false };
  }
}

Important Notes

  • The function must return { success: true } after processing to acknowledge the webhook. Failing to do so may result in Shopify retrying the webhook, which could cause duplicate processing.
  • On the JsRates Demo Plan, each incoming webhook request counts toward the 1,000 request quota limit. Be mindful of this limit when testing or handling frequent webhook events.

Logs & Testing

  • Received webhook data is logged automatically.
  • You can view logs under More actions > Logs in your JsRates app Editor page.
  • In Shopify Admin, you can test your webhook by clicking the three dots next to the webhook trigger you created and selecting Send test notification.

Potential Applications of processShopifyWebhook

The processShopifyWebhook function provides customization and automation options in JsRates, beyond what Shopify offers by default. Here are some potential applications:

  • Send Order Data to Custom Carrier APIs
    Automatically forward order details (e.g., from orders/paid) to your carrier's API or 3PL system for fulfillment.

  • Trigger Dynamic Shipping Label Creation
    Generate custom shipping labels in real-time after an order is created or paid.

  • Automate Pickup Scheduling
    Notify carriers to schedule pickups when orders are fulfilled or paid.

  • Send Custom Notifications
    Notify customers via email or SMS about order status updates, using data received in the webhook.

  • Sync Data with CRMs and ERPs
    Send order and fulfillment details to external systems like CRMs or ERPs for seamless data flow.